<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

require_once(APPPATH . '/controllers/super_admin' . EXT);
class Cms extends Super_admin {

    function __construct() {
        parent::__construct();

        $this->load->model(array(
            'User_model',
            'Information_model'
        ));

        $this->load->helper(array(
            'security',
            'string',
        ));
    }

    function index() {
        $access = array('admin'); //admin only
        $this->_is_authorize($access); //check super_admin controller

        $this->list_records();
    }

    /**
     * list_records
     *
     * Load information list
     *
     * @access public
     * @param void
     * @return void
     */
    function list_records() {
        $access = array('admin'); //admin only
        $this->_is_authorize($access); //check super_admin controller

        $this->load->view('cms/list');
    }
    
    function get_cms() {
        
        $where = array();
        $select = "*";

        $page = $this->input->post('page');
        $order_by = $this->input->post('sidx') . ' ' . $this->input->post('sord');

        if ($this->input->post('sidx') == 'category')
            $order_by .= ', sub_category';

        $total = $this->Information_model->get_total($where);
        $num_pages = ceil($total / LIMIT);

        if ($page > $num_pages && $total <> 0) {
            $page = $num_pages;
        }

        $offset = (LIMIT * $page) - LIMIT;

        $args = array(
                    'select' => $select,
                    'where' =>  array(),
                    'join' => array(),
                    'limit' => LIMIT,
                    'offset' => $offset,
                    'order_by' => $order_by,
                    'resource_id' => TRUE,
        );

        $records = $this->Information_model->get_list($args);

        $jsn_records = '[]';
        if ($total > 0) {
            $jsn_records = $this->json_formatter->_run($records, 'information_id');
        }

        echo '{"page": "' . $page . '", "total": "' . $num_pages . '", "records": "' . $total . '", "rows": ' . $jsn_records . '}';
    }
    
    function add(){
        $access = array('admin'); //admin only
        $this->_is_authorize($access); //check super_admin controller

        $data = array(
            'information_id' => 0,
            'information_type' => 'How It Works',
            'content' => '',
 
        );
        
        $this->load->view('cms/form', $data);
        
    }
    
    function edit($id){
        
        $params = array(
                'select' => '*',
                'where' => array('information_id' => $id),
                'join' => array(),
                'order_by' => NULL
        );

        $data = $this->Information_model->get_record($params);

        $this->load->view('cms/form', $data);
        
        
    }
    
    function view($id){
        $access = array('admin'); //admin only
        $this->_is_authorize($access); //check super_admin controller
        
        $params = array(
                'select' => '*',
                'where' => array('information_id' => $id),
                'join' => array(),
                'order_by' => NULL
        );

        $data = $this->Information_model->get_record($params);

        $this->load->view('cms/display', $data);
        
        
    }
    
    function submit(){
        
        $response = FALSE;
        $information_id = $this->input->post('information_id', TRUE);

        $data = array(
            'information_type' => $this->input->post('content_type',TRUE),
            'content' => $this->input->post('content',TRUE)

        );


        if (empty($information_id)) {

            $data['date_created'] = pdate();
            $data['created_by'] = _session('user_id');

            $this->Information_model->_insert($data);

            $msg = sprintf(lang('cms_add_success'), "Content");
            $response = TRUE;

        } else {
            $data['date_modified'] = pdate();
            $data['modified_by'] = _session('user_id');

            $where = array('information_id' => $information_id);

            $this->Information_model->_update($where, $data);

            $msg = sprintf(lang('cms_edit_success'), "Content");


            $response = TRUE;
        }

        if ($response == TRUE)
            redirect('cms');

    }
    
    function delete($information_id) {
        $access = array('admin'); //admin only
        $this->_is_authorize($access); //check super_admin controller
        
        _success(sprintf(lang('cms_delete_success'), "Content"));

        $this->Information_model->_delete(array('information_id' => $information_id));

        $msg = sprintf(lang('cms_delete_success'), "Content");

        $response = TRUE;
        _success($msg, FALSE);
        echo json_encode($response);
    }

  

}
/* End of file cms.php */
/* Location: ./application/controllers/cms.php */
